//
//  UIImage+SnapShot.m
//  Perking
//
//  Created by longmin on 16/7/11.
//  Copyright © 2016年 tim. All rights reserved.
//

#import "UIImage+SnapShot.h"

@implementation UIImage (SnapShot)

+(instancetype)screenShotWithView:(UIView *)view forRect:(CGRect)rect{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.width, view.height), YES, 0);     //设置截屏大小
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef imageRef = viewImage.CGImage;
    CGFloat scale = [UIScreen mainScreen].bounds.size.width == 414 ? 3 : 2;
    CGRect cutRect = CGRectMake(rect.origin.x *scale, rect.origin.y *scale, rect.size.width *scale, rect.size.height *scale);//这里可以设置想要截图的区域
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, cutRect);
    UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
    return sendImage;
}

+(instancetype)imageWithSnapShotView:(UIView *)view {
    UIGraphicsBeginImageContext(view.bounds.size);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

@end
